package com.px.web.controller.system;

import com.px.common.constant.Constants;
import com.px.common.core.controller.BaseController;
import com.px.common.core.domain.AjaxResult;
import com.px.common.core.domain.model.LoginBody;
import com.px.common.core.domain.model.PhoneCode;
import com.px.common.utils.StringUtils;
import com.px.framework.aes.AesSecurityParameter;
import com.px.framework.web.service.SysLoginService;
import com.px.system.service.IPhoneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 飞行用户信息Controller
 * 
 * @author WRJ
 * @date 2024-08
 */
@Api("手机验证码")
@RestController
@RequestMapping("/system/phone")
public class PhoneController extends BaseController
{
    @Autowired
    private IPhoneService phoneService;

    @Autowired
    private SysLoginService loginService;

    @ApiOperation("发送手机验证码")
    @PostMapping(value = "/sendCode")
    @AesSecurityParameter(outEncode = true)
    public AjaxResult sendCode(@RequestBody PhoneCode phoneCode)
    {
        String msg = phoneService.sendCode(phoneCode);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    @AesSecurityParameter(outEncode = true)
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.phoneLogin(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getType());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }


}
